/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.gestion_mouvement;

import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author ANNAS
 */
public class MouvementValidatorDame extends MouvementValidator{

    public MouvementValidatorDame() {
        this.longueurChemin = Integer.MAX_VALUE;
    }
    
    protected void validationMouvement(){
        try
        {
            if(getArrive().hasPiece())
                    throw new Exception("La case destination nest pas vide!");
            List<ICase> chemin = getChemin();
            if(chemin.size()>1)
            {
                ICase courante=null;
                ListIterator iter = (ListIterator)chemin.listIterator();
                while(iter.hasNext())
                {
                   courante = (ICase)iter.next();
                   if(courante.hasPiece())
                   {
                       IPiece p = courante.getPiece();
                       if(p.getCouleur()==getCouleur())
                            throw new Exception("La dame ne peut pas manger un ami!");
                       int indexPrecedent = iter.previousIndex()-1;
                       if(indexPrecedent>=0)
                       {
                           ICase precedente = chemin.get(indexPrecedent);
                               if(precedente.hasPiece())
                                    throw new Exception("La dame ne peut pas manger 2 consécutifs!");
                       }
                       this.pieceAManger.add(courante.getPiece());
                   }
                  
                }
            }
        }
        catch(Exception e)
        {
            this.valid = false;
            this.pieceAManger.clear();
            return;
        }
        this.valid = true;
    }
    
}
